package com.ruoyi.web.controller.weiriya;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.weiriya.domain.WNotice;
import com.ruoyi.weiriya.service.IWNoticeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 平台公告Controller
 * 
 * @author lynn
 * @date 2023-08-01
 */
@Api(tags = "平台公告管理")
@RestController
@RequestMapping("/weiriya/notice")
public class WNoticeController extends BaseController
{
    @Autowired
    private IWNoticeService wNoticeService;

    /**
     * 查询平台公告列表
     */
    @ApiOperation("获取平台公告列表")
    @GetMapping("/list")
    public TableDataInfo list(WNotice wNotice)
    {
        startPage();
        List<WNotice> list = wNoticeService.list(wNotice.queryWrapper());
        return getDataTable(list);
    }

    /**
     * 导出平台公告列表
     */
    @ApiOperation("导出平台公告列表")
    @PreAuthorize("@ss.hasPermi('weiriya:notice:export')")
    @Log(title = "平台公告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WNotice wNotice)
    {
        List<WNotice> list = wNoticeService.list(wNotice.queryWrapper());
        ExcelUtil<WNotice> util = new ExcelUtil<WNotice>(WNotice.class);
        util.exportExcel(response, list, "平台公告数据");
    }

    /**
     * 导入平台公告列表
     */
    @ApiOperation("导入平台公告列表")
    @PreAuthorize("@ss.hasPermi('weiriya:notice:import')")
    @Log(title = "平台公告", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<WNotice> util = new ExcelUtil<WNotice>(WNotice.class);
        List<WNotice> list = util.importExcel(file.getInputStream());
        return success(wNoticeService.saveBatch(list));
    }

    /**
     * 获取平台公告详细信息
     */
    @ApiOperation("获取平台公告详细")
    @ApiImplicitParam(name = "id", value = "平台公告ID", required = true, dataType = "String", paramType = "path")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(wNoticeService.getById(id));
    }

    /**
     * 新增平台公告
     */
    @ApiOperation("新增平台公告")
    @PreAuthorize("@ss.hasPermi('weiriya:notice:add')")
    @Log(title = "平台公告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WNotice wNotice)
    {
        String details = wNotice.getDetails();
        if(StringUtils.isNotBlank(details) && details.length() > 2000){
            throw new ServiceException("公告内容长度不能超过2000个字符！");
        }
        return toAjax(wNoticeService.save(wNotice));
    }

    /**
     * 修改平台公告
     */
    @ApiOperation("更新平台公告")
    @PreAuthorize("@ss.hasPermi('weiriya:notice:edit')")
    @Log(title = "平台公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WNotice wNotice)
    {
        return toAjax(wNoticeService.updateById(wNotice));
    }
    /**
     * 修改平台公告状态
     */
    @ApiOperation("修改平台公告状态")
    @PreAuthorize("@ss.hasPermi('weiriya:notice:edit')")
    @Log(title = "平台公告", businessType = BusinessType.UPDATE)
    @PutMapping("/updateState")
    public AjaxResult editState(@RequestBody WNotice wNotice)
    {
        WNotice byId = wNoticeService.getById(wNotice.getId());
        byId.updateState();
        return toAjax(wNoticeService.updateById(byId));
    }

    /**
     * 删除平台公告
     */
    @ApiOperation("删除平台公告")
    @ApiImplicitParam(name = "ids", value = "平台公告ID集合", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('weiriya:notice:remove')")
    @Log(title = "平台公告", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids)
    {
        return toAjax(wNoticeService.removeByIds(ids));
    }
}
